package me.hys.action.company;

import java.util.Map;

import me.hys.base.AjaxResult;
import me.hys.base.BaseAction;
import me.hys.base.PageQuery;
import me.hys.entity.company.ProductEntity;
import me.hys.service.company.IProductManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Author: @author HYS . <br>
 * Date: 2015-9-14 上午12:22:24 . <br>
 * Function: 产品管理
 */
@Controller
@RequestMapping("/product")
public class ProductAction extends BaseAction {
	@Autowired
	private IProductManager productManager;

	public ProductAction() {
		setFlag(getClass().getSimpleName());
	}

	/**
	 * @Title : add
	 * @Description : 添加页面
	 */
	@RequestMapping("/add")
	public ModelAndView add() {
		Map<String, Object> data = getMap();
		data.put("entity", new ProductEntity());
		return new ModelAndView("/company/product_form", data);
	}

	/**
	 * @Title : delete
	 * @Description : 删除
	 */
	@RequestMapping("/del/{id}")
	@ResponseBody
	public AjaxResult delete(@PathVariable String id) {
		productManager.delete(id);
		return new AjaxResult();
	}

	/**
	 * @Title : edit
	 * @Description : 修改页面
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable String id) {
		Map<String, Object> data = getMap();
		data.put("entity", productManager.findById(id));
		return new ModelAndView("/company/product_form", data);
	}

	/**
	 * @Title : index
	 * @Description : 产品列表
	 */
	@RequestMapping("/index")
	public ModelAndView index() {
		Map<String, Object> data = getMap();
		PageQuery<ProductEntity> pageQuery = getPageQuery();
		data.put("data", productManager.findPage(pageQuery));
		return new ModelAndView("/company/product", data);
	}

	/**
	 * @Title : push
	 * @Description : 推送
	 */
	@ResponseBody
	@RequestMapping(value = "/push/{id}", method = RequestMethod.GET)
	public void push(@PathVariable String id) {
		productManager.push(id);
	}

	/**
	 * @Title : save
	 * @Description : 保存
	 */
	@RequestMapping("/save")
	@ResponseBody
	public AjaxResult save(@ModelAttribute ProductEntity entity,
			@RequestHeader(value = "referer") String url) {
		entity.setPubuser(getUser().getId());
		productManager.saveOrUpdate(entity);
		AjaxResult ajaxResult = new AjaxResult();
		ajaxResult.setMsg(url);
		return ajaxResult;
	}
}
